//
//  BLCommon_Sheet.m
//  BLNew
//
//  Created by 乔小乔 on 2020/4/29.
//  Copyright © 2020 chehang168. All rights reserved.
//

#import "BLCommon_Sheet.h"
#import "BLCommon_Cell_Sheet.h"

#import "BLSheetModel.h"

static NSString *reuse_BLCommon_Cell_Sheet = @"Reuse_BLCommon_Cell_Sheet";
static NSString *reuse_UITableViewCell = @"Reuse_UITableViewCell";
@interface BLCommon_Sheet()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) UIView *viewContent;
@end

@implementation BLCommon_Sheet
{
    
    CGFloat maxHeight;
    NSArray *dataArray;
    BLCommon_Model_AlertItem *cancelItem;
    void (^myBlock)(BLCommon_Model_AlertItem *item);
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor chColorWithHexString:@"#000000" alpha:0];
        [self createUI];
    }
    return self;
}
- (void)blockClickItem:(void(^)(BLCommon_Model_AlertItem *item))over{
    if (myBlock!=over) {
        myBlock = over;
    }
}
- (void)show{
    UIWindow *window = [[UIApplication sharedApplication]keyWindow];
    [window addSubview:self];
}

- (void)createUI{
    [self addSubview:self.viewContent];
    [self.viewContent addSubview:self.myTableView];
}

-(void)reloadWithArray:(NSArray*)array withCancelModel:(BLCommon_Model_AlertItem *)candelModel{
    cancelItem = candelModel;
    dataArray = array;
    if (array.count > 6) {
         maxHeight = 6*50+BL_BOTTOM_MARGIN+60;
    }else{
         maxHeight = array.count*50+BL_BOTTOM_MARGIN+60;
    }
    
    self.viewContent.frame = CGRectMake(0, BL_WINDOW_HEIGHT, BL_WINDOW_WIDTH, maxHeight);
    self.myTableView.frame = CGRectMake(0, 0, BL_WINDOW_WIDTH, maxHeight-BL_BOTTOM_MARGIN);
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.viewContent.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(16,16)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.viewContent.bounds;
    maskLayer.path = maskPath.CGPath;
    self.viewContent.layer.mask = maskLayer;
    
    [self.myTableView reloadData];
    
    [self showAnimate];
}
-(void)showAnimate{
    [UIView animateWithDuration:0.3 animations:^{
        self.viewContent.center = CGPointMake(BL_WINDOW_WIDTH/2, BL_WINDOW_HEIGHT-maxHeight/2);
        self.backgroundColor =  [UIColor colorWithRed:27/255.0 green:28/255.0 blue:51/255.0 alpha:0.7];
    }];
}

-(void)hideAnimate{
    [UIView animateWithDuration:0.3 animations:^{
        self.viewContent.center = CGPointMake(BL_WINDOW_WIDTH/2, BL_WINDOW_HEIGHT+maxHeight/2);
        self.backgroundColor =  [UIColor clearColor];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (dataArray.count>0) {
        return dataArray.count+2;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == dataArray.count) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse_UITableViewCell];
        cell.backgroundColor = [UIColor chColorWithHexString:@"#EDEDF0"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    BLCommon_Cell_Sheet *cell = [tableView dequeueReusableCellWithIdentifier:reuse_BLCommon_Cell_Sheet];
    if (indexPath.row<=dataArray.count-1) {
        BLCommon_Model_AlertItem *dic = dataArray[indexPath.row];
        
        [cell reloadWithDic:dic];
    }else{
        [cell reloadWithDic:cancelItem];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == dataArray.count) {
        return 10;
    }
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == dataArray.count) {
        return ;
    }
    BLCommon_Model_AlertItem *dic = [[BLCommon_Model_AlertItem alloc]init];
    if (indexPath.row<=dataArray.count-1) {
        dic = dataArray[indexPath.row];
        if (myBlock) {
            myBlock(dic);
        }
        if ([self.delegate respondsToSelector:@selector(BLCommon_SheetClickIndex:withModel:)]) {
            [self.delegate BLCommon_SheetClickIndex:indexPath.row withModel:dic];
        }
    }
    [self hideAnimate];
}

#pragma mark - setter
- (UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, BL_WINDOW_WIDTH, BL_WINDOW_HEIGHT, 0) style:UITableViewStylePlain];
        _myTableView.estimatedRowHeight = 0;
        _myTableView.estimatedSectionFooterHeight = 0;
        _myTableView.estimatedSectionHeaderHeight = 0;
        _myTableView.showsVerticalScrollIndicator = NO;
        _myTableView.separatorColor = [UIColor chColorWithHexString:@"#EDEDF0"];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorInset = UIEdgeInsetsMake(0, 16, 0, 16);
        _myTableView.tableFooterView = [UIView new];
        [_myTableView registerNib:[UINib nibWithNibName:@"BLCommon_Cell_Sheet" bundle:nil] forCellReuseIdentifier:reuse_BLCommon_Cell_Sheet];
        [_myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:reuse_UITableViewCell];
    }
    return _myTableView;
}

- (UIView *)viewContent{
    if (!_viewContent) {
        _viewContent = [[UIView alloc]init];
        _viewContent.backgroundColor = [UIColor whiteColor];
    }
    return _viewContent;
}
@end
